std::literals::string_literals:: operator""s
| 
           定义于头文件
            
            
             <string>
            
            | ||
| 
           
           
           
            
             
              std::
              
               string
              
             
            
            operator
            
             ""
            
            s
            
             (
            
            
             const
            
            
             char
            
            
             *
            
            str,
            
             
              std::
              
               size_t
              
             
            
            len
            
             )
            
            
             ;
            
           
           | (1) | (C++14 起) (C++20 起为 constexpr) | 
| 
           
            
             constexpr
            
            
             
              std::
              
               u8string
              
             
            
            operator
            
             ""
            
            s
            
             (
            
            
             const
            
            char8_t
            
             *
            
            str,
             std:: size_t len ) ; | (2) | (C++20 起) | 
| 
           
           
           
            
             
              std::
              
               u16string
              
             
            
            operator
            
             ""
            
            s
            
             (
            
            
             const
            
            
             char16_t
            
            
             *
            
            str,
            
             
              std::
              
               size_t
              
             
            
            len
            
             )
            
            
             ;
            
           
           | (3) | (C++14 起) (C++20 起为 constexpr) | 
| 
           
           
           
            
             
              std::
              
               u32string
              
             
            
            operator
            
             ""
            
            s
            
             (
            
            
             const
            
            
             char32_t
            
            
             *
            
            str,
            
             
              std::
              
               size_t
              
             
            
            len
            
             )
            
            
             ;
            
           
           | (4) | (C++14 起) (C++20 起为 constexpr) | 
| 
           
           
           
            
             
              std::
              
               wstring
              
             
            
            operator
            
             ""
            
            s
            
             (
            
            
             const
            
            
             wchar_t
            
            
             *
            
            str,
            
             
              std::
              
               size_t
              
             
            
            len
            
             )
            
            
             ;
            
           
           | (5) | (C++14 起) (C++20 起为 constexpr) | 
形成所需类型的字符串字面量。
| 目录 | 
参数
| str | - | 指向原始字符数组字面量起始位置的指针 | 
| len | - | 原始字符数组字面量的长度 | 
返回值
字符串字面量。
注释
       这些运算符声明于命名空间
       
        
         std
         
          ::
         
         
          literals
         
         
          ::
         
         
          string_literals
         
        
       
       中,其中
       
        literals
       
       与
       
        string_literals
       
       均为内联命名空间。可通过以下任意
       
        
         using
        
       
       指令访问这些运算符:
      
- using namespace std :: literals
- using namespace std :: string_literals
- using namespace std :: literals :: string_literals
std::chrono::duration 还定义了 operator""s 来表示字面量秒数,但它是算术字面量: 10.0s 和 10s 表示十秒,而 "10" s 是字符串。
| 功能测试 宏 | 值 | 标准 | 功能 | 
|---|---|---|---|
| 
           __cpp_lib_string_udls
           | 
           201304L
           | (C++14) | 字符串类型的用户定义字面量 | 
示例
#include <iostream> #include <string> void print_with_zeros(const auto note, const std::string& s) { std::cout << note; for (const char c : s) c ? std::cout << c : std::cout << "₀"; std::cout << " (size = " << s.size() << ")\n"; } int main() { using namespace std::string_literals; std::string s1 = "abc\0\0def"; std::string s2 = "abc\0\0def"s; print_with_zeros("s1: ", s1); print_with_zeros("s2: ", s2); std::cout << "abcdef"s.substr(1,4) << '\n'; }
输出:
s1: abc (size = 3) s2: abc₀₀def (size = 8) bcde
参见
| 构造 
          basic_string
         对象(公开成员函数) | |
| 
           
            
             
              (C++17)
             
            
           
           | 创建字符数组字面量的字符串视图 (函数) |